const axios = require('axios');

// 配置请求基础路径
const BASE_URL = 'http://localhost:3000';

// 模拟前端数据 - 包含所有必要字段
const frontEndData = {
  id:'3',
  orderId: '60',
  consigneeName: '测试用户',
  phoneNumber: '13800138000',
  detailAddress: '测试详细地址',
  deliveryTime: '2025-09-17 13:00-17:00',
  remarks: '测试备注',
  province: '',
  city: '',
  district: '',
   created_at: '2025-09-16T06:51:47.000Z',
    updated_at: '2025-09-16T06:51:47.000Z'
};

// 模拟前端参数转换逻辑
const formatData = (data) => {
  return {
    id: data.id || '',
    order_id: data.orderId || '',
    consignee_name: data.consigneeName || '',
    phone_number: data.phoneNumber || '',
    province: '',
    city: '',
    district: '',
    detail_address: data.detailAddress || '',
    remarks: data.remarks || '',
    delivery_time: data.deliveryTime || '',
     created_at: data.created_at || '',
    updated_at: data.updated_at || ''
  };
};

// 完整测试函数
async function testFullFrontendRequest() {
  console.log('==================== 完整前端请求流程测试 ====================');
  console.log('前端原始数据:', frontEndData);
  
  // 格式化数据
  const formattedData = formatData(frontEndData);
  console.log('格式化后的数据:', formattedData);
  
  try {
    // 测试带斜杠的路径
    console.log('\n测试1: 使用路径 "/api/order_address/" (带斜杠)');
    const response1 = await axios.post(`${BASE_URL}/api/order_address/`, formattedData, {
      headers: {
        'Content-Type': 'application/json'
      }
    });
    console.log('测试1结果:', response1.data);
    
    // 测试不带斜杠的路径
    console.log('\n测试2: 使用路径 "/api/order_address" (不带斜杠)');
    const response2 = await axios.post(`${BASE_URL}/api/order_address`, formattedData, {
      headers: {
        'Content-Type': 'application/json'
      }
    });
    console.log('测试2结果:', response2.data);
    
  } catch (error) {
    console.error('测试失败:', error.response?.data || error.message);
  }
}

// 运行测试
async function runTests() {
  try {
    await testFullFrontendRequest();
    console.log('\n==================== 测试完成 ====================');
  } catch (error) {
    console.error('整体测试失败:', error);
  }
}

// 执行测试
runTests();